﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Himp.ShareData;

namespace Himp.Erp
{
    /// <summary>
    /// 药品目录扩展信息
    /// </summary>
    public class DrugCatalogExtService : CrudAppService<DrugCatalogExt, DrugCatalogExtDto, Guid, DrugCatalogExtInput, DrugCatalogExtCreateOrUpdateDto, DrugCatalogExtCreateOrUpdateDto>, IDrugCatalogExtService
    {
        private IRepository<DrugCatalogExt, Guid> _DrugCatalogExtRepository;
        private IDrugCatalogService _drugCatalogService;
        private IDrugPrdrService _drugPrdrService;
        private IMatlInfoService _matlInfoService;
        private IDrugCatalogTypeService _drugCatalogTypeService;
        private IPubItemService _pubItemService;

        public DrugCatalogExtService(IRepository<DrugCatalogExt, Guid> DrugCatalogExtRepository
            , IDrugPrdrService drugPrdrService
            , IMatlInfoService matlInfoService
            , IDrugCatalogTypeService drugCatalogTypeService
            , IDrugCatalogService drugCatalogService
            , IPubItemService pubItemService) : base(DrugCatalogExtRepository)
        {
            _DrugCatalogExtRepository = DrugCatalogExtRepository;
            _drugPrdrService = drugPrdrService;
            _matlInfoService = matlInfoService;
            _drugCatalogTypeService = drugCatalogTypeService;
            _drugCatalogService = drugCatalogService;
            _pubItemService = pubItemService;
        }

        /// <summary>
        /// 获取药品目录扩展信息列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<DrugCatalogExtDto>> GetListAsync(DrugCatalogExtInput input)
        {
            var iQuery = await _DrugCatalogExtRepository.WithDetailsAsync();
            iQuery = iQuery.WhereIf(input.DrugCatalogId != null, i => i.DrugCatalogId == input.DrugCatalogId);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<DrugCatalogExt>, List<DrugCatalogExtDto>>(iQuery.ToList());

                return new PagedResultDto<DrugCatalogExtDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<DrugCatalogExt>, List<DrugCatalogExtDto>>(items.ToList());

                return new PagedResultDto<DrugCatalogExtDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 创建药品目录扩展信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<DrugCatalogExtDto> CreateAsync(DrugCatalogExtCreateOrUpdateDto input)
        {
            var drugPrdr = await _drugPrdrService.GetAsync(input.DrugPrdrId);
            input.DrugPrdrId = input.DrugPrdrId;

            var drugCatalog = await _drugCatalogService.GetAsync(input.DrugCatalogId);
            var drugCatalogType = await _drugCatalogTypeService.GetAsync(drugCatalog.DrugCatalogTypeId);

            #region IMatlInfoService

            MatlInfoCreateDto matlInfoCreateDto = new MatlInfoCreateDto();
            matlInfoCreateDto.MatlCatgeCodg = drugCatalogType.MatlCatgeCodg;
            matlInfoCreateDto.MatlCatgeName = drugCatalogType.MatlCatgeName;
            matlInfoCreateDto.MatlInfoCodg = Guid.NewGuid().ToString();
            matlInfoCreateDto.MatlInfoName = drugCatalog.DrugName;
            matlInfoCreateDto.ProdentpName = drugPrdr.PrdrName;
            matlInfoCreateDto.Aprvno = input.Aprvno;
            matlInfoCreateDto.SaluntUntCodg = input.MinSaluntCode;
            matlInfoCreateDto.SaluntUntName = input.MinSaluntName;
            matlInfoCreateDto.PacCnt = input.MinPacCnt;
            matlInfoCreateDto.PacuntCodg = input.MinPacuntCode;
            matlInfoCreateDto.PacuntName =input.MinPacuntName;
            matlInfoCreateDto.Spec = input.DrugSpec;
            matlInfoCreateDto.Prodplac = input.Scdz;
            matlInfoCreateDto.Srtno = 0;
            await _matlInfoService.CreateAsync(matlInfoCreateDto);

            #endregion

            #region PubItemService  

            PubItemCreateDto pubItemCreateDto = new PubItemCreateDto();
            pubItemCreateDto.PubItemCatgeCodg = drugCatalogType.PubItemCatgeCodg;
            pubItemCreateDto.PubItemCatgeName = drugCatalogType.PubItemCatgeName;
            pubItemCreateDto.ClncChrgitmTypeCode = drugCatalog.ChrgitmType?.ClncChrgitmTypeCode;
            pubItemCreateDto.ClncChrgitmTypeName = drugCatalog.ChrgitmType?.ClncChrgitmTypeName;
            pubItemCreateDto.PubItemName = drugCatalog.DrugName;
            pubItemCreateDto.Spec = input.DrugSpec;
            pubItemCreateDto.Unt = input.DrugSpec;
            pubItemCreateDto.Pric = input.RtalPric ?? 0;
            pubItemCreateDto.PriceCode = "-";
            pubItemCreateDto.SrtNo = 0;
            pubItemCreateDto.DrugFlag = true;
            await _pubItemService.PostAsync(pubItemCreateDto);

            #endregion

            return await base.CreateAsync(input);
        }
    }
}
